<?php
	if (!defined('BASEPATH'))
		exit('No direct script access allowed');

	class RecoverPassword extends CI_Controller
	{
		public function index()
		{
			$this->load->view('v_recoverpassword');
		}

		public function recover()
		{
			$number = rand(16324583,97857334);
			$cryptPassword = crypt("$number");
			$this->load->model('m_user', 'currentUser');
			$this->currentUser->set_user_email($_POST['Email']);
			$this->currentUser->set_user_password($cryptPassword);
			$this->error_code = $this->currentUser->reset_password();

			$this->load->library('email');

			$this->email->from('donotreply@cecs492.com', 'Billing Service');
			$this->email->to($_POST['Email']);
			$this->email->subject('Password Reset');

			$emailMessage = "Your password has been reset to " . "$number";

			$this->email->message($emailMessage);
			$this->email->send();

			// echo $this->email->print_debugger();
			// go back to home screen
			$message = 'A password reset email has been sent to ' . $_POST['Email'] . '.';
			$data['message'] = $message;
			$this->load->view('v_home', $data);
		}
	}
?>
